package com.group.yztcedu.playlive.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.group.yztcedu.playlive.R;
import com.group.yztcedu.playlive.base.BaseActivity;
import com.group.yztcedu.playlive.main.found.FoundFragment;
import com.group.yztcedu.playlive.main.home.HomeFragment;
import com.group.yztcedu.playlive.main.me.MeFragment;
import com.group.yztcedu.playlive.widget.MyRadioButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.home_toolbar)
    Toolbar homeToolbar;
    @BindView(R.id.iv_index_play)
    ImageView ivIndexPlay;
    @BindView(R.id.btn_home)
    MyRadioButton btnHome;
    @BindView(R.id.btn_found)
    MyRadioButton btnFound;
    @BindView(R.id.btn_me)
    MyRadioButton btnMe;
    @BindView(R.id.home_rg)
    RadioGroup homeRg;
    @BindView(R.id.home_vp)
    ViewPager homeVp;
    private List<Fragment> fragments;
    private HomeFragment homeFragment;
    private FoundFragment foundFragment;
    private MeFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment();
        initControoler();
        homeRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.btn_home:
                        homeVp.setCurrentItem(0);
                        break;
                    case R.id.btn_found:
                        homeVp.setCurrentItem(1);
                        break;
                    case R.id.btn_me:
                        homeVp.setCurrentItem(2);
                        break;

                }
            }
        });

    }

    private void initControoler() {
        homeVp.setAdapter(new RadioGroupPagerAdapter(getSupportFragmentManager(), fragments));
        homeVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        homeRg.check(R.id.btn_home);
                        break;
                    case 1:
                        homeRg.check(R.id.btn_found);
                        break;
                    case 2:
                        homeRg.check(R.id.btn_me);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    private void initFragment() {
        fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        foundFragment = new FoundFragment();
        meFragment = new MeFragment();
        fragments.add(homeFragment);
        fragments.add(foundFragment);
        fragments.add(meFragment);
    }
    @OnClick({R.id.home_toolbar, R.id.iv_index_play})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_toolbar:
                startActivity(new Intent(this,ToolbarNavigationActivity.class));
                break;
            case R.id.iv_index_play:
                startActivity(new Intent(this,IndexPlayActivity.class));
                break;
        }
    }
}
